package com.example.demo101.config;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

import java.io.IOException;

/**
 * TE
 *
 * @author lichao
 * @version 1.0.0
 * @since 2025/9/4 14:51
 */
public class TE {
    public static void main(String[] args) throws Exception {
        // 此处改为你实际的的 MyBatisCodeHelper-Pro-obfuss.jar 的路径
        String pathname = "D:\\zfp\\instrumented-MyBatisCodeHelper-Pro241-3.4.6+2321.jar";
        String classname = "com.ccnode.codegenerator.ag.d.f";
        // 将修改后的Class b写入指定文件夹
        String directoryName = "D:\\zfp";
        extracted(pathname,classname,directoryName);
    }

    private static void extracted( String pathname,String classname,String directoryName) throws NotFoundException, CannotCompileException, IOException {
        ClassPool pool = ClassPool.getDefault();

        pool.insertClassPath(pathname);
        // 通过绝对定位，加载指定的类

        CtClass cc = pool.get(classname);

        // 获取validTo的get方法
        CtMethod getValidToMethod = cc.getDeclaredMethods()[3];

        // 获取validTo的set方法的参数：Long
        CtClass[] params = new CtClass[]{pool.get("java.lang.Long")};

        // 获取validTo的set方法
        CtMethod setValidToMethod =cc.getDeclaredMethods()[7];

        // 修改validTo的get方法
        // 直接返回4797976044000
        StringBuilder builder = new StringBuilder();
        builder.append("{")
                .append("   return 4794976044000L;")
                .append("}");
        getValidToMethod.setBody(builder.toString());

        // 修改validTo的set方法
        // 直接设为4797976044000
        StringBuilder builder1 = new StringBuilder();
        builder1.append("{")
                .append("   this.a= 4794976044000L;")
                .append("}");
        setValidToMethod.setBody(builder1.toString());




        // 获取valid的set方法的参数：Boolean
        CtClass[] params1 = new CtClass[]{pool.get("java.lang.Boolean")};
        // 获取Valid的set方法
        // public void a(Boolean bl)
        CtMethod setValidMethod = cc.getDeclaredMethods()[5];

        // 获取valid的get方法
        // public Boolean b()
        CtMethod getValidMethod =cc.getDeclaredMethods()[1];



        // 修改valid的set方法
        // 设为True
        String getValidMethodBuilder = "{" +
                "       return true;" +
                "}";
        getValidMethod.setBody(getValidMethodBuilder);

        // 修改valid的get方法
        // 直接返回True
        String setValidMethodBuilder = "{" +
                " this.a = true;" +
                "}";
        setValidMethod.setBody(setValidMethodBuilder);


        cc.writeFile(directoryName);
    }
}
